home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 8: LINUX Games / Linux Cubed Series 8 - LINUX Games.iso / games / video / thrust-s.53 / thrust-s / thrust / datasrc / bin2c.c next >
C/C++ Source or Header  |  1995-10-13  |  1KB  |  74 lines

  1.  
  2. /* Written by Peter Ekberg, peda@lysator.liu.se */
  3.  
  4. #include <unistd.h>
  5. #include <sys/types.h>
  6. #include <stdio.h>
  7.  
  8. #define LL (8)
  9.  
  10. unsigned char buf[LL+1];
  11.  
  12. void quit(char *str)
  13. {
  14.   perror(str);
  15.   exit(1);
  16. }
  17.  
  18. int writebuf(int count)
  19. {
  20.   int i;
  21.   
  22.   if(printf("\t") != 1)
  23.     return(1);
  24.   for(i=0; i<count-1; i++)
  25.     if(printf("0x%02x, ", buf[i]) != 6)
  26.       return(1);
  27.   if(i<count)
  28.     if(printf("0x%02x", buf[i]) != 4)
  29.       return(1);
  30.   return(0);
  31. }
  32.  
  33. int main(int argc, char *argv[])
  34. {
  35.   int stat;
  36.   int end=0;
  37.  
  38.   if(argc!=2) {
  39.     fprintf(stderr, "%s: Usage '%s variable_name'\n",
  40.         argv[0],
  41.         argv[0]);
  42.     exit(1);
  43.   }
  44.   if(printf("\nunsigned char %s[] = {\n", argv[1]) != (22 + strlen(argv[1])))
  45.     quit(argv[0]);
  46.   stat=fread(buf+LL, 1, 1, stdin);
  47.   if(stat!=1) {
  48.     if(ferror(stdin))
  49.       quit(argv[0]);
  50.     end=1;
  51.   }
  52.   while(!end) {
  53.     *buf=*(buf+LL);
  54.     stat=fread(buf+1, 1, LL, stdin);
  55.     if(stat!=LL) {
  56.       if(ferror(stdin))
  57.     quit(argv[0]);
  58.       end=1;
  59.       if(writebuf(stat+1))
  60.     quit(argv[0]);
  61.     }
  62.     else {
  63.       if(writebuf(LL))
  64.     quit(argv[0]);
  65.       if(printf(",\n") != 2)
  66.     quit(argv[0]);
  67.     }
  68.   }  
  69.   if(printf(" };\n") != 4)
  70.     quit(argv[0]);
  71.  
  72.   return(0);
  73. }
  74.